﻿#region

using Microsoft.AspNetCore.Http;
using Vin.Extension.Captcha.Service.Interface;
using Vin.Extension.Framework.Login.Attributes;
using Vin.Extension.Framework.Login.Dto.Entities;
using Vin.Extension.Framework.Login.ServiceInterface;
using Vin.Tool.AspNetCore.Service;
using Vin.Tool.Attributes;
using Vin.Tool.Core.AssertCategory;
using Vin.Tool.Core.EnumCategory;
using Vin.Tool.Domain.ExceptionModel;

#endregion

namespace Vin.Extension.Framework.Login.Service.LoginService;

[VinLoginService(VinLoginType.Password, VinLoginHandleType.Password, false, true, Way = "账号密码登录")]
public class AccountLoginService : VinBaseService, ILoginService
{
    [Inject] private readonly ISysLoginService _sysLoginService = null!;

    [Inject] private readonly ISysAccountService _sysAccountService = null!;

    [Inject] private readonly ISysPasswordService _sysPasswordService = null!;

    [Inject] private readonly IVinGenerateCaptchaService _vinGenerateCaptchaService = null!;

    [Inject] private readonly IVinRandomCodeService _vinRandomCodeService = null!;

    public async Task<object> VerifyAsync(HttpContext context)
    {
        var uuid = context.Request.Query["uuid"];
        _vinGenerateCaptchaService.ClearCaptcha(uuid);
        return await Task.FromResult(
            _vinGenerateCaptchaService.GenerateCaptcha(_vinRandomCodeService.CreateRandomCode(4)));
    }

    public async Task<string> LoginAsync(HttpContext context)
    {
        var loginBody = await context.Request.ReadFromJsonAsync<LoginBodyDto>();
        try
        {
            // 验证码
            if (string.IsNullOrEmpty(loginBody!.Uuid) ||
                !_vinGenerateCaptchaService.VerifyCaptcha(loginBody!.Uuid, loginBody.Code))
            {
                throw new BusinessException("验证码错误");
            }

            return await PasswordLoginAsync(loginBody);
            // // 查询账号是否存在，并组品信息
            // var account = await _sysAccountService.QueryByAccountAsync(loginBody.UserName);
            // EmptyTool.CheckIsNull(account, "账号不存在");
            //
            // // 账号是否被禁用
            // if (account!.IsEnabled == false) throw new BusinessException("账号已被禁用");
            //
            // // 验证密码
            // if (!_sysPasswordService.Verify(loginBody.Password, account.Password))
            //     throw new BusinessException("密码不匹配");
            //
            // // 记录登录成功日志
            // return await _sysLoginService.CreateTokenAsync(account, VinLoginType.Password.GetName(), loginBody);
        }
        catch (System.Exception e)
        {
            // 记录登录失败日志
            await _sysLoginService.AddLoginLogAsync(null, loginBody?.UserName, false, e.Message,
                VinLoginType.Password.GetName(),
                loginBody);
            throw;
        }
    }

    public Task<bool> BindAsync(HttpContext context)
    {
        throw new BusinessException("暂不支持绑定");
    }

    public Task<bool> UnBindAsync(HttpContext context)
    {
        throw new BusinessException("暂不支持解绑");
    }

    public async Task<string> PasswordLoginAsync(LoginBodyDto loginBody)
    {
        // 查询账号是否存在，并组品信息
        var account = await _sysAccountService.QueryByAccountAsync(loginBody.UserName);
        EmptyTool.CheckIsNull(account, "账号不存在");

        // 账号是否被禁用
        if (account!.IsEnabled == false) throw new BusinessException("账号已被禁用");

        // 验证密码
        if (!_sysPasswordService.Verify(loginBody.Password, account.Password))
            throw new BusinessException("密码不匹配");

        // 记录登录成功日志
        return await _sysLoginService.CreateTokenAsync(account, VinLoginType.Password.GetName(), loginBody);
    }
}